package com.partvoyage.server.dao;

import java.util.List;
import java.util.logging.Logger;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.QueryResultIterable;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class DataInfoPointDAO extends ObjectifyDAO<DataInfoPointEntity> {

	private static final Logger LOG = Logger.getLogger(DataInfoPointDAO.class
			.getName());

	static {
		ObjectifyService.register(DataInfoPointEntity.class);
	}

	public DataInfoPointDAO() {
		super(DataInfoPointEntity.class);

	}

	public void commit() {
		ofy().getTxn().commit();
	}

	public List<DataInfoPointEntity> listDataInfoPointByProjetKey(
			Key<ProjetEntity> projetKey) {
		return asList(getChildren(projetKey));
	}

	public List<DataInfoPointEntity> listDataInfoPointByProjetKeyAndProperty(
			Key<ProjetEntity> projetKey, String propName, Object propValue) {
		return asList(getChildren(projetKey, propName, propValue));
	}

	// http://code.google.com/p/objectify-appengine/wiki/AdvancedPatterns#Ancestor_Queries
	private QueryResultIterable<DataInfoPointEntity> getChildren(
			Key<ProjetEntity> projetKey, String propName, Object propValue) {
		if (projetKey == null)
			throw new IllegalArgumentException();
		return ofy().query(clazz).ancestor(projetKey)
				.filter(propName, propValue).fetch();
	}

	private QueryResultIterable<DataInfoPointEntity> getChildren(
			Key<ProjetEntity> projetKey) {
		if (projetKey == null)
			throw new IllegalArgumentException();
		return ofy().query(clazz).ancestor(projetKey).fetch();
	}

}